﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.IO;

namespace Bing.Toolkit.Web
{
    /// <summary>
    /// Json序列化,返序列化
    /// </summary>
    public static class JSON
    {
        /// <summary>
        /// 将对像序列化JSON字符串
        /// </summary>
        /// <param name="obj">对像</param>
        /// <returns>Json String</returns>
        public static String Stringify(object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            
            return serializer.Serialize(obj);
        }

        /// <summary>
        /// 将JSON字符串返序列化对像
        /// </summary>
        /// <typeparam name="ReturnType">对像类型</typeparam>
        /// <param name="json">字符串</param>
        /// <returns>对像</returns>
        public static ReturnType Parse<ReturnType>(string json)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            return serializer.Deserialize<ReturnType>(json);
        }
    }
}
